# !/usr/bin/env python3
"""
    xpan auth
    include:
        authorization_code, just get token by code
        refresh_token
        device_code
"""
import os,sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
import openapi_client
from openapi_client.api import auth_api
from pprint import pprint


def oauthtoken_authorizationcode():
    """
    authorizationcode
    get token by authorization code
    """
    # Enter a context with an instance of the API client
    with openapi_client.ApiClient() as api_client:
        # Create an instance of the API class
        api_instance = auth_api.AuthApi(api_client)
        code = "" # str |
        client_id = "" # str |
        client_secret = "" # str |
        redirect_uri = "oob" # str | 

        # example passing only required values which don't have defaults set
        try:
            api_response = api_instance.oauth_token_code2token(code, client_id, client_secret, redirect_uri)
            pprint(api_response)
        except openapi_client.ApiException as e:
            print("Exception when calling AuthApi->oauth_token_code2token: %s\n" % e)


def oauthtoken_refreshtoken(refresh_token,client_id,client_secret):
    """
    refresh access token
    """
    # Enter a context with an instance of the API client
    with openapi_client.ApiClient() as api_client:
        # Create an instance of the API class
        api_instance = auth_api.AuthApi(api_client)

        # example passing only required values which don't have defaults set
        try:
            api_response = api_instance.oauth_token_refresh_token(refresh_token, client_id, client_secret)
            pprint(api_response)
            return api_response
        except openapi_client.ApiException as e:
            return ("Exception when calling AuthApi->oauth_token_refresh_token: %s\n" % e)


def oauthtoken_devicecode():
    """
    devicecode 
    get device code 
    """
    # Enter a context with an instance of the API client
    with openapi_client.ApiClient() as api_client:
        # Create an instance of the API class
        api_instance = auth_api.AuthApi(api_client)
        #client_id = "TVt69cI4bFaCdVERodvG4AetZ0k62FzL" # str |
        scope = "basic,netdisk" # str | 

        # example passing only required values which don't have defaults set
        try:
            api_response = api_instance.oauth_token_device_code(client_id, scope)
            return(api_response)
        except openapi_client.ApiException as e:
            return ("Exception when calling AuthApi->oauth_token_device_code: %s\n" % e)


def oauthtoken_devicetoken():
    """
    get token by device code
    """
# Enter a context with an instance of the API client
    with openapi_client.ApiClient() as api_client:
        # Create an instance of the API class
        api_instance = auth_api.AuthApi(api_client)
        code = "a9144104d7ca70725f6b823acf425eb3" # str |
        client_id = "TVt69cI4bFaCdVERodvG4AetZ0k62FzL" # str |
        client_secret = "xOaz1xRvbEoNFHZh8gE5wZzPambZXApj" # str |

        # example passing only required values which don't have defaults set
        try:
            api_response = api_instance.oauth_token_device_token(code, client_id, client_secret)
            pprint(api_response)
        except openapi_client.ApiException as e:
            print("Exception when calling AuthApi->oauth_token_device_token: %s\n" % e)

if __name__ == '__main__':
    """
    main
    """
    #oauthtoken_authorizationcode()
    #oauthtoken_refreshtoken()

    print(oauthtoken_devicecode())

    print(oauthtoken_devicetoken())

    #print(oauthtoken_refreshtoken('126.dfd71540bcae903dca98424973a6dff7.YnYopuLSMgY2Z4Kvnm9e_C4-bReeLmwjQjMXL45.jHJE3g',"TVt69cI4bFaCdVERodvG4AetZ0k62FzL","xOaz1xRvbEoNFHZh8gE5wZzPambZXApj"))
#b'{"expires_in":2592000,"refresh_token":"122.723e8dbda175952fc511bd4941bcd1e5.Y3pZ6iyj3laG_1pKKm6_GwwQhCC-Wmn_7J2Y8K5.U_4IIg","access_token":"121.053965f8c4babe193300fb2462b9db65.YQebbxHtnzBnxEUrEquw7blDJfDY6HfXuTVQNqQ.rB1Dlg","session_secret":"","session_key":"","scope":"basic netdisk"}\n'

